package com.xh.core.controller;

import com.xh.core.bean.AjaxResult;
import com.xh.core.constant.AjaxResultConstant;
import com.xh.core.util.CacheUtils;
import com.xh.core.util.SendMsgUtils;
import com.xh.core.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/*
 * @Author daiwei
 * @Description
 * @Date 2019/6/11 14:38
 **/
@RestController
@RequestMapping("/sms")
public class SmsController {

    @PostMapping("/sendCheckCode")
    public AjaxResult sendCheckCode(@RequestParam String token,@RequestParam String checkCode,@RequestParam String phone){
        String result=null;
        if(StringUtils.isEmpty(checkCode)){
            return new AjaxResult(AjaxResultConstant.Result.ERROR,"验证码不能为空");
        }
        if(StringUtils.isEmpty(phone)){
            return new AjaxResult(AjaxResultConstant.Result.ERROR,"手机号不能为空");
        }
        boolean checkCodeResult=false;
        Object value = CacheUtils.get(token);
        if (value != null && checkCode.equals(value)) {
            checkCodeResult= true;
        }

        if(checkCodeResult) {
            String code = StringUtils.randomNumber(4);
            result = SendMsgUtils.sendCheckCode(phone,code);
            return new AjaxResult(true,result);
        }else{
            result = "验证码错误";
            return new AjaxResult(result==null,result);
        }

     }
}
